package com.weiyi.mashangzhu.Landlord.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Landlord.Bean.FacilityBean_L;
import com.weiyi.mashangzhu.Landlord.Bean.NextBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Bean.FlagBean;

import java.util.ArrayList;
import java.util.List;

import static com.weiyi.mashangzhu.Application.MyApp.flagBeanList;

/**
 * Created by win7 on 2017/9/25.
 */

public class FacilityAdapter_L extends BaseAdapter {
    List<FacilityBean_L> list;
    Context context;

    public FacilityAdapter_L(List<FacilityBean_L> list, Context context) {
        this.list = list;
        this.context = context;
            for (int i = 0; i < list.size(); i++) {
                MyApp.flagBeanList.add(new FlagBean(false));
        }

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_next, null);
            viewHolder.count = (TextView) convertView.findViewById(R.id.tv);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(viewHolder);

        }
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.count.setText(list.get(position).getName());

            if (MyApp.flagBeanList == null || MyApp.flagBeanList.size() == 0) {
                viewHolder.imageView.setImageResource(list.get(position).getImg());
            } else {
                if (MyApp.flagBeanList.get(position).isFlag() == true) {
                    viewHolder.imageView.setImageResource(R.mipmap.duo);
                } else {
                    viewHolder.imageView.setImageResource(R.mipmap.duo1);
                }
            }
            final ViewHolder finalViewHolder1 = viewHolder;
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (MyApp.flagBeanList.get(position).isFlag() == false) {
                        finalViewHolder1.imageView.setImageResource(R.mipmap.duo);
                        MyApp.flagBeanList.get(position).setFlag(true);
                    } else {
                        finalViewHolder1.imageView.setImageResource(R.mipmap.duo1);
                        MyApp.flagBeanList.get(position).setFlag(false);
                    }
                }
            });
//        }else {
//            viewHolder.imageView.setImageResource(list.get(position).getImg());
//            final ViewHolder finalViewHolder2 = viewHolder;
//            convertView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (MyApp.flagBeanList_edit.get(position).isFlag() == false) {
//                        finalViewHolder2.imageView.setImageResource(R.mipmap.duo);
//                        MyApp.flagBeanList_edit.get(position).setFlag(true);
//                    } else {
//                        finalViewHolder2.imageView.setImageResource(R.mipmap.duo1);
//                        MyApp.flagBeanList_edit.get(position).setFlag(false);
//                    }
//                }
//            });
//        }

        return convertView;
    }

    class ViewHolder {
        TextView count;
        ImageView icon, imageView;
    }
}
